﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using System;
using System.IO;
using System.Linq;
using UnityEngine;

namespace LS.Editor.DataTableTools
{
    public sealed partial class DataTableProcessor
    {
        private sealed class QuaternionProcessor : GenericDataProcessor<Quaternion>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "Quaternion";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "quaternion",
                    "unityengine.quaternion"
                };
            }

            public override Quaternion Parse(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return Quaternion.identity;
                }
                string[] splitedValue = value.Split(',');
                return new Quaternion(float.Parse(splitedValue[0]), float.Parse(splitedValue[1]), float.Parse(splitedValue[2]), float.Parse(splitedValue[3]));
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                Quaternion quaternion = Parse(value);
                binaryWriter.Write(quaternion.x);
                binaryWriter.Write(quaternion.y);
                binaryWriter.Write(quaternion.z);
                binaryWriter.Write(quaternion.w);
            }
        }
        private sealed class QuaternionArrayProcessor : GenericDataArrayProcessor<Quaternion>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "Quaternion[]";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "quaternion[]",
                    "unityengine.quaternion[]"
                };
            }

            public override Quaternion ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return Quaternion.identity;
                }
                string[] splitedValue = value.Split(',');
                return new Quaternion(float.Parse(splitedValue[0]), float.Parse(splitedValue[1]), float.Parse(splitedValue[2]), float.Parse(splitedValue[3]));
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var array = Parse(value);
                binaryWriter.Write(array.Length);
                for (int i = 0; i < array.Length; i++)
                {
                    Quaternion quaternion = array[i];
                    binaryWriter.Write(quaternion.x);
                    binaryWriter.Write(quaternion.y);
                    binaryWriter.Write(quaternion.z);
                    binaryWriter.Write(quaternion.w);
                }
            }
        }
        private sealed class QuaternionListProcessor : GenericDataListProcessor<Quaternion>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "List<Quaternion>";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "list<quaternion>",
                    "list<unityengine.quaternion>",
                    "system.collections.generic.list<quaternion>",
                    "system.collections.generic.list<unityengine.quaternion>",
                };
            }

            public override Quaternion ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return Quaternion.identity;
                }
                string[] splitedValue = value.Split(',');
                return new Quaternion(float.Parse(splitedValue[0]), float.Parse(splitedValue[1]), float.Parse(splitedValue[2]), float.Parse(splitedValue[3]));
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var array = Parse(value);
                binaryWriter.Write(array.Count);
                for (int i = 0; i < array.Count; i++)
                {
                    Quaternion quaternion = array[i];
                    binaryWriter.Write(quaternion.x);
                    binaryWriter.Write(quaternion.y);
                    binaryWriter.Write(quaternion.z);
                    binaryWriter.Write(quaternion.w);
                }
            }
        }
        private sealed class QuaternionDictionaryProcessor : GenericDataDictionaryProcessor<Quaternion>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "Dictionary<string,Quaternion>";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "dictionary<string,quaternion>",
                    "dictionary<string,unityengine.quaternion>",
                    "dictionary<system.string,Quaternion>",
                    "dictionary<system.string,unityengine.quaternion>",
                    "system.collections.generic.dictionary<string,quaternion>",
                    "system.collections.generic.dictionary<string,unityengine.quaternion",
                    "system.collections.generic.dictionary<system.string,quaternion>",
                    "system.collections.generic.dictionary<system.string,unityengine.quaternion>",
                };
            }

            public override Quaternion ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return Quaternion.identity;
                }
                string[] splitedValue = value.Split(',');
                return new Quaternion(float.Parse(splitedValue[0]), float.Parse(splitedValue[1]), float.Parse(splitedValue[2]), float.Parse(splitedValue[3]));
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var dic = Parse(value);
                binaryWriter.Write(dic.Count);
                foreach (var item in dic)
                {
                    binaryWriter.Write(item.Key);
                    Quaternion quaternion = item.Value;
                    binaryWriter.Write(quaternion.x);
                    binaryWriter.Write(quaternion.y);
                    binaryWriter.Write(quaternion.z);
                    binaryWriter.Write(quaternion.w);
                }
            }
        }
    }
}
